import { observable, action, autorun, toJS } from 'mobx';
import { WeaTools, WeaLocaleProvider } from 'ecCom';
import { message, Modal } from 'antd';
import * as Apis from '../apis/highSeas';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export default class highSeasApproveListStore {
    @observable tableStore = new TableStore();
    @observable loading = false;
    @observable searchConditionForm = new WeaForm();
    @observable searchConditionComponents = {};
    @observable showSearchAd = false;
    @observable searchBaseParams = {};
    @observable showLeft = true;
    @observable rightMenu = [];
    @observable seasId = '';
    @observable seasTree = [];
    @observable CrmName = '';// 客户名称
    @observable customerIds = '';// 勾选的客户ID
    @observable showModal = false; // 弹框显示
    @observable treeData = [];
    @observable countsType = [];
    @observable counts = {};
    @observable logDialogVisible = false;
    @observable logType = '';
    @observable buttonDisabled = false;

    @action
    initDatas = (params = {}) => {
      this.seasId = '';
      this.seasTree = [];
      this.treeData = [];
      this.CrmName = '';
      this.customerIds = '';
      this.countsType = [];
      this.counts = {};
      this.searchBaseParams = {};
      this.tableStore = new TableStore();
      this.searchConditionForm.reset();
      this.loading = true;
      this.getTableData({ ...params, onlysessionkey: false });
      this.getSeasTree();
      this.logType = '3';
    }

    @action
    doSearch = () => {
      this.tableStore = new TableStore();
      const that = this;
      this.loading = true;
      this.setShowSearchAd(false);
      const searchParamsAd = this.searchConditionForm.getFormParams();
      // searchParamsAd里没有uncontact，uncontact_1时，需要将searchBaseParams里的值清掉
      if (!searchParamsAd.uncontact) {
        delete that.searchBaseParams.uncontact;
      }
      if (!searchParamsAd.uncontact_1) {
        delete that.searchBaseParams.uncontact_1;
      }
      const requestParams = { ...this.searchBaseParams, ...searchParamsAd, seasId: this.seasId, onlysessionkey: true };
      for (const item in requestParams) {
        if (requestParams[item] == '') {
          delete requestParams[item];
          delete that.searchBaseParams[item];
        }
      }
      this.searchBaseParams = { ...this.searchBaseParams, ...requestParams };
      this.getTableData(this.searchBaseParams);
    }

    getTableData = (params = {}) => {
      Apis.seasDispenseList(params).then((data) => {
        if (data.status == '0') {
          message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
        } else {
          this.tableStore.getDatas(data.datas, 1);
          if (!params.onlysessionkey) {
            this.searchConditionComponents = data.formItemGroupList;
            this.searchConditionForm.initFormFields(this.searchConditionComponents);

            this.rightMenu = data.rightMenu;
          }
        }
        this.loading = false;
      });
    }

    @action
    tableReload = () => {
      this.tableStore.getDatas(this.tableSessionKey, this.tableStore.current);
    }

    @action
    setShowSearchAd = (bool = false) => {
      this.showSearchAd = bool;
    }

    clearAdFields = () => {
      this.searchConditionForm.reset();
    }

    @action
    setSearchKey = (v = '') => {
      try {
        this.searchBaseParams.name = v;
        const searchKeyField = {};
        searchKeyField.name = v;
        this.searchConditionForm.updateFields(searchKeyField, false);
      } catch (error) {
        // 高级搜索中没有searchKey的情况
      }
    }

    @action
    searchConditionFieldChange = (fieldObj) => {
      if (fieldObj.name) {
        this.searchBaseParams = { ...this.searchBaseParams, name: fieldObj.name.value };
      }
    }

    @action
    isShowLeft = (bl) => {
      this.showLeft = bl;
    }

    @action
    controlOperateDialog = (bool, operate, askfn) => {
      // console.log(askfn);
    }

    @action
    getSeasTree = (params = {}) => {
      Apis.seasDispenseTree(params).then((data) => {
        if (data.status == '0') {
          message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
        } else {
          this.treeData = data.datas;
          this.countsType = data.countsType;
          this.counts = data.counts;
        }
      });
    }

    @action
    seasId = (seasId) => {
      this.seasId = seasId;
    }

    // 显示弹窗
    @action
    showCustomerModal = (bool, ids) => {
      this.buttonDisabled = true;
      // console.log(this.buttonDisabled);
      this.customerIds = ids;
      this.showModal = bool;
      if (bool) {
        Apis.getNameByseasCustomerid({ seasCustomerId: ids }).then((data) => {
          this.CrmName = data.datas;
        });
      }
    }
    // 分配客户经理
    @action
    saveCustomer=(params = {}) => {
      this.loading = true;
      this.buttonDisabled = true;
      Apis.dispenseCustomer(params).then((data) => {
        this.customerIds = '';
        this.tableReload();
        this.getSeasTree();
        this.loading = false;
        this.buttonDisabled = false;
      });
    }
    @action
    showLog = () => {
      this.logDialogVisible = true;
    }

    @action
    changeLogDialog = () => {
      this.logDialogVisible = false;
    }

    @action
    resetParams = () => {
      for (const item in this.searchBaseParams) {
        delete this.searchBaseParams[item];
      }
      this.searchConditionForm.reset();
    }
}
